package lib.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.directory.SearchControls;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import atg.taglib.json.util.JSONArray;
import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;

import lib.util.BookOperator;

import object.Book;
import object.User;

/**
 * Servlet implementation class BookQueryServlet
 */
@WebServlet("/BookQueryServlet")
public class BookQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookQueryServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			response.setCharacterEncoding("UTF_8");
			response.setHeader("Content-type", "text/xml;charset=UTF-8");
			int queryType = Integer.parseInt(request.getParameter("queryType"));
			switch (queryType) {
			case 0:
				searchBookByName(request, response);
				break;
			case 1:

			default:
				break;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void searchBookByName(HttpServletRequest request,
			HttpServletResponse response) throws SQLException, JSONException, IOException {

		String user_id = ((User) request.getSession().getAttribute("user"))
				.getUser_id();
		String book_name = request.getParameter("book_name");
		int page = Integer.parseInt(request.getParameter("page"));
		ArrayList<Book> books = BookOperator.searchByName(book_name, page,
				user_id);
		int totalNum = BookOperator.lastNum;

		JSONArray allJsonObject = books2Json(books, totalNum);
		response.getWriter().print(allJsonObject);
		response.getWriter().flush();
	}

	private JSONArray books2Json(ArrayList<Book> books, int totalNum)
			throws JSONException {
		JSONArray allJsonObject = new JSONArray();
		for (Book book : books) {
			JSONObject newJson = new JSONObject();
			newJson.put("book_name", book.getBook_name());
			newJson.put("writer", book.getWriter());
			newJson.put("publisher", book.getPublisher());
			newJson.put("publish_date", book.getPublish_date().toString());
			newJson.put("book_code", book.getBook_code());
			newJson.put("type", book.getType());
			newJson.put("only_teach", book.isOnly_teach());
			newJson.put("shelf_num", book.getShelf_num());
			newJson.put("total_book_num", book.getTotal_book_num());
			newJson.put("book_exist", book.isBook_exist());
			newJson.put("book_on_shelf", book.isBook_on_shelf());
			newJson.put("library_name", book.getLibrary_name());
			newJson.put("location", book.getLocation());
			newJson.put("total_pages", totalNum / BookOperator.PAGESIZE);
			newJson.put("book_id", book.getBook_id());
			newJson.put("is_reserved_by_cur", book.isReserve());
			allJsonObject.add(newJson);
		}
		return allJsonObject;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
